< previous page page_101 next page >

Page 101
The procedure name for an event procedure contains the name of the object and the name of the event. In this example, Form is the object and MouseMove is the event. Notice that the MouseMove event procedure also contains several parameters; for example, the parameters X and Y represent the coordinates of the mouse.
5. At this point, you can write program statements to take whatever action(s) should happen in response to an occurrence of the event. Add the following line of code to the event procedure:
Me.Caption = "Mouse Coordinates are (" & X & "," & Y & ")"
Your event procedure should now look like this:
Private Sub Form_MouseMove(Button As Integer, 
Shift As Integer, X As Single, Y As Single)
    Me.Caption = "Mouse Coordinates are (" & X & "," & Y & ")"
End Sub
The preceding code displays the values of the X and Y parameters in the form's title bar by setting the Caption property of the form. Press Visual Basic's Start button, or F5, and move the mouse pointer over the form. You should see the form's caption (title) change as the mouse pointer moves.
tip.gif
If you enter a Code window by double-clicking an object, the Code window automatically selects that object's most commonly used event procedure. For example, if you double-click a command button at designtime, the Code window is opened to that command button's Click event procedure.

Calling an Event Procedure from Code
As you learn Visual Basic, you will be writing and calling many procedures. Event procedures are kind of special in that they are called by Visual Basic itself. In the MouseMove example in the preceding section, Visual Basic executed the code you wrote in response to an event. However, it is noteworthy to mention that you can execute event procedures yourself, just like any other procedure you would write. You might do so when you have written code in an event procedure that needs to be executed in multiple places.
Let me demonstrate this by example. If you still have the Standard EXE project open from the previous example, return to Design mode and draw a CommandButton control on the form. Double-click the command button and change the event procedure to display the current date and time, as follows:
Private Sub Command1_Click()
    MsgBox "The system time is " & Now
End Sub
The preceding code is easy to understand. It just displays a message box when the button Command1 is clicked. However, you can call the Command1_Click procedure from other parts of your code, including another event. Double-click the form itself, and from the Procedure box, select the Click event. Change the Form_Click event procedure to call the Command1_Click procedure:

 
< previous page page_101 next page >